package com.fyzk.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;

import http.DataUtil.text.TextUtil;

/**
 * <p>
 *
 * @author Administrator
 * @mood
 * @description
 * @date 2018/8/8 10:46
 * </p>
 */
public class FootTitleView extends RelativeLayout implements View.OnClickListener {
    private Context context;
    private ImageView leftBtn;
    private TextView centerTxt;
    private TextView rightTxt;
    private ImageView rightImg;
    private ImageView rightImg2;
    private ImageView title_iv;//向下箭头
    private RelativeLayout rl;
    private LinearLayout center_ll;


    private String centerTxtStr = "";
    private String rightTxtStr = "";
    private int rightVisibleInt;
    private int leftVisibleInt;
    private int rightImgResurce;
    private int backgroundColor;
    private int backgroundDrawble;

    private Animation anim;

    public FootTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init(attrs);
    }

    public FootTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    public FootTitleView(Context context) {
        super(context);
        this.context = context;
        init(null);
    }

    /**
     * 初始化View
     */
    private void init(AttributeSet attrs) {
        if (null != attrs) {
            TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.TopTitleView);
            centerTxtStr = type.getString(R.styleable.TopTitleView_CenterText);
            rightTxtStr = type.getString(R.styleable.TopTitleView_RightTxt);
            rightVisibleInt = type.getInt(R.styleable.TopTitleView_rightVisible, View.INVISIBLE);
            leftVisibleInt = type.getInt(R.styleable.TopTitleView_leftVisible, 0);
            rightImgResurce = type.getResourceId(R.styleable.TopTitleView_rightImg, 0);
            backgroundColor = type.getResourceId(R.styleable.TopTitleView_backgroundColor, 0);
            backgroundDrawble = type.getResourceId(R.styleable.TopTitleView_backgroundDrawable, 0);
        }
        View.inflate(context, R.layout.layout_football, this);
        rl = (RelativeLayout) findViewById(R.id.topview_title_rl);
        centerTxt = (TextView) findViewById(R.id.topview_top_tv_title);
        rightTxt = (TextView) findViewById(R.id.topview_top_txt_right);
        center_ll = (LinearLayout) findViewById(R.id.topview_center_ll);
        leftBtn = (ImageView) findViewById(R.id.topview_top_iv_back);
        rightImg = (ImageView) findViewById(R.id.topview_top_iv_right);
        rightImg2 = (ImageView) findViewById(R.id.topview_top_iv_right2);
        title_iv = (ImageView) findViewById(R.id.dj_top_title_iv);
        if (null != centerTxtStr && !centerTxtStr.isEmpty()) {
            centerTxt.setText(centerTxtStr);
        }
        switch (rightVisibleInt) {
            case View.INVISIBLE:
                rightTxt.setVisibility(View.INVISIBLE);
                rightImg.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                rightTxt.setVisibility(View.GONE);
                rightImg.setVisibility(View.GONE);
                break;
            case View.VISIBLE:
                if (rightImgResurce != 0) {
                    rightImg.setVisibility(View.VISIBLE);
                    rightTxt.setVisibility(View.GONE);
                    rightImg.setImageResource(rightImgResurce);
                } else {
                    rightImg.setVisibility(View.GONE);
                    rightTxt.setVisibility(View.VISIBLE);
                    if (!TextUtil.isEmpty(rightTxtStr)) {
                        rightTxt.setText(rightTxtStr);
                    }
                }

                break;
        }
        switch (leftVisibleInt) {
            case View.INVISIBLE:
                leftBtn.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                leftBtn.setVisibility(View.GONE);
                break;
            case View.VISIBLE:
                leftBtn.setVisibility(View.VISIBLE);
                break;
        }

        rightTxt.setOnClickListener(this);
        rightImg.setOnClickListener(this);
        rightImg2.setOnClickListener(this);
        leftBtn.setOnClickListener(this);
        center_ll.setOnClickListener(this);
        if(backgroundColor == 0 && backgroundDrawble == 0){
            rl.setBackgroundColor(this.getContext().getResources().getColor(R.color.top_bg1));
        }else if(backgroundDrawble != 0){
            this.setBackground(backgroundDrawble);
        }else{
            this.setBackground(backgroundDrawble);
        }

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (isInEditMode()) {
            return;
        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            ViewGroup.LayoutParams params = this.getLayoutParams();
//            params.height = Utils.dip2px(75);
//            this.setLayoutParams(params);
//            this.setPadding(0, Utils.dip2px(25), 0, 0);
//            if(backgroundColor == 0 && backgroundDrawble == 0){
//                this.setBackgroundColor(this.getContext().getResources().getColor(R.color.top_bg));
//            }else if(backgroundDrawble != 0){
//                this.setBackground(backgroundDrawble);
//            }else{
//                this.setBackground(backgroundColor);
//            }
//        }
    }

    public ImageView getRightImgCom() {
        return rightImg;
    }

    public void setJianTouVisible(int restId) {
        title_iv.setVisibility(restId);
        if (restId == View.VISIBLE) {
            center_ll.setClickable(true);
        } else {
            center_ll.setClickable(false);
        }
    }

    public void setJianTouRotate_shun() {
        title_iv.startAnimation(AnimationUtils.loadAnimation(context, R.anim.title_jiantou_shun));
    }

    public void setJianTouRotate_ni() {
        title_iv.startAnimation(AnimationUtils.loadAnimation(context, R.anim.title_jiantou_ni));
    }

    public void setRightTxt(String text) {
        rightTxt.setVisibility(View.VISIBLE);
        rightImg.setVisibility(View.GONE);
        rightImg2.setVisibility(View.GONE);
        rightTxt.setText(text);
    }

    public TextView getRightTxt() {
        return rightTxt;
    }

    public RelativeLayout getRl(){
        return rl;
    }


    //    public void setCenterClickable(boolean clickable){
//        center_ll.setClickable(clickable);
//    }

    /**
     * 设置右边按钮的图片
     *
     * @param resId
     */
    public void setRightImg(int resId) {
        rightImg.setImageResource(resId);
    }

    /**
     * 设置右边按钮2的图片
     *
     * @param resId
     */
    public void setRight2Img(int resId) {
        rightImg2.setImageResource(resId);
    }


    /**
     * @param type View.VISIBLE;View.INVISIBLE;View.GONE
     */
    public void setLeftVisible(int type) {
        leftBtn.setVisibility(type);
    }

    /**
     * @param type View.VISIBLE;View.INVISIBLE;View.GONE
     */
    public void setRightVisible(int type) {
        rightImg.setVisibility(type);
    }

    public void setRightTxtVisible(int type){
        rightTxt.setVisibility(type);
    }

    /**
     * @param type View.VISIBLE;View.INVISIBLE;View.GONE
     */
    public void setRight2Visible(int type) {
        rightImg2.setVisibility(type);
    }

    private onTopTitleClickListener listener;

    public void setOnTitleClickListener(onTopTitleClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.topview_center_ll:
                if (null != listener) {
                    listener.onCenterClick();
                }
                break;
            case R.id.topview_top_iv_back:
                if (null != listener) {
                    listener.onLeftClick();
                }
                break;
            case R.id.topview_top_txt_right:
            case R.id.topview_top_iv_right:
                if (null != listener) {
                    listener.onRightClick();
                }
                break;

            case R.id.topview_top_iv_right2:
                if (null != listener) {
                    listener.onRight2Click();
                }
                break;

            default:
                break;
        }
    }

    public View getRightView() {
        return rightImg;
    }

    public ImageView getRightImg2() {
        return rightImg2;
    }

    public void setCenterText(String text) {
        centerTxt.setText(text);
    }

    public void setCenterText(int text) {
        centerTxt.setText(text);
    }

    public void setBackground(int resid) {
        rl.setBackgroundResource(resid);
    }

    public void startRightAnim() {
        if (anim == null) {
            anim = AnimationUtils.loadAnimation(context, R.anim.rotate);
            rightImg2.setAnimation(anim);
        }
        rightImg2.startAnimation(anim);
    }

}
